// 04myCenter/11shoppingOrder/shoppingOrder.js
var co = require('../../resource/js/common.js')
var M = require("../../resource/js/md5.js")
var _this, _data
var app = getApp()
var pageNum = 1
Page({
   /*页面的初始数据*/
   data: {
      taps: [{
            name: '全部',
            id: 0
         },
         {
            name: '待付款',
            id: 1
         },
         {
            name: '已付款',
            id: 2
         },
         {
            name: '已发货',
            id: 5
         },
         {
            name: '已完成',
            id: 7
         },
         {
            name: '已评价',
            id: 4
         }         
      ],
      id: '',
      showOrder: [],
      order: [],
      realHeight: '',
      totalPrice: [],
      info: '',
      isShow: false,
      url: '',
      payMode: false, //选择支付方式模态框
      pasInput: false, //余额支付密码输入框
      hint: '',
      money:'',//付款金额
      userInfo:'',//用户信息
      orderID:'',//订单ID
   },
   /*生命周期函数--监听页面加载*/
   onLoad: function(options) {
      _this = this
      _data = this.data
      console.log('订单状态ID数字',options)
      this.setData({
         userInfo:app.Data.userInfo,
         id: options.index
      })
      // 获取点击后传递的id值
      var id = parseInt(options.index)
      //console.log('id',id)
      this.getOrderStatus(id)
      var sys = wx.getSystemInfoSync()
      
      this.setData({
         realHeight: sys.windowHeight
      })
   },   
   // 获取导航条点击id
   getOrederStatus(event) {
      var idx = event.currentTarget.dataset.index
      _this.setData({
         id: idx,         
      })
      //console.log("【存入的id值】", _this.data.id)
      var id = parseInt(idx)
      pageNum = 1 //切换标签时，页码置零
      _this.setData({
         order:[]
      })
      //_data.order = []
      _this.getOrderStatus(id)
   },
   // 获取数据
   getOrderStatus(id) {      
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         pageNo: pageNum,
         Order_Status: id ? id : _data.id
      }
      co.getMealApi('jyhs/Ordmg/getOrderList', param, function(res) {
         // console.log("【获取我的订单状态】===",res.data)
         if (res.code == '0') {
            pageNum++
            var allList = _data.order.concat(res.data)
            _this.setData({
               order: allList,
               isShow: false,
               showOrder: []
            }) //存入order
            // console.log("【获取Order_CartList】===", res.data)
            for (let i = 0; i < res.data.length; i++) {
               let map = res.data[i].Order_CartList
               let list = _this.data.showOrder
               list.push(map)
               _this.setData({
                  showOrder: list
               })
            }
         } else if (res.code == 29300) {
            _this.setData({               
               hint:true,
            })
         }
      })
   },
   // 查询快递
   expressCheck: function(e) {
      var idx = e.currentTarget.dataset.id,
         order = JSON.stringify(this.data.order[idx]);
      //console.log("【idx====】", order)
      wx: wx.navigateTo({
         url: '01expressCheck/expressCheck?&order=' + order,
      })
   },   
   onUnload: function() {
      pageNum = 1
   },
   //取消订单
   cancalOrder: function(e) {
      var order_ID = e.currentTarget.dataset.orderid
      var status = 3         
      _this.changeOrderStatus(order_ID, status)
   },
   //付款
   payOrder: function(e) {
      //console.log('e',e)
      var order_ID = e.currentTarget.dataset.orderid
      var money = e.currentTarget.dataset.money     
      this.setData({
         money: money,
         orderID:order_ID,
         payMode: true
      })
      
      //_this.changeOrderStatus(order_ID, status)
   },
   //收货
   confirmOrder: function(e) {
      var order_ID = e.currentTarget.dataset.orderid
      var status = 7
      _this.changeOrderStatus(order_ID, status)
   },
   //评价


   //更改订单状态
   changeOrderStatus: function(orderID, status) {
      var url = 'jyhs/Ordmg/updateStatus'
      var param = {
         Order_ID: orderID,
         Order_Status: status
      }
      co.getMealApi(url, param, function(res) {
         if (res.code == 0) { 
            co.toastAllText('操作成功')
            pageNum = 1
            _this.setData({
               order:[]
            })                     
            _this.getOrderStatus()
         }
      })

   },
   //查看订单详情
   checkOrderDetail: function(e) {
      console.log(e)
      var orderID = e.currentTarget.dataset.orderid
      wx.navigateTo({
         url: '/04myCenter/11shoppingOrder/01spOrderDetail/spOrderDetail?orderID=' + orderID,
      })
   },
   /**
    * 页面上拉触底事件的处理函数
    */
   getother: function() {      
      this.getOrderStatus()
   },
   // 选择的支付方式
   payMode: function (e) {
      var mode = e.currentTarget.dataset.mode
      var userInfo = app.Data.userInfo
      if (mode == 1) {
         //判断是否设置支付密码
         if (userInfo.PayPassword_Is_Set == 0) {
            co.confirm('未设置支付密码', '取消', '前往设置', function (res) {
               if (res) {
                  wx.navigateTo({
                     url: "/04myCenter/02set/02bindPhone/bindPhone",
                  })
               }
            })
            return
         }
         //如果余额为零自动跳转为微信支付
         if (userInfo.User_Balance == 0) {
            co.toast('您的账户余额为零，请充值')
            return
         }
         if (parseInt(userInfo.User_Balance) < parseInt(this.data.total)) {
            co.toast('您的余额不足，请充值')
            return
         }
         this.setData({
            payMode: false,
            pasInput: true
         })
      } else if (mode == 2) {         
         this.pay(_data.money)         
         this.setData({
            payMode: false
         })
      }
   },
   //关闭弹窗
   payClose: function () {      
      _this.setData({
         pasInput: false,
         payMode : false
      })      
   },
   //添加用户支付记录
   addUserRecord: function (callback) {
      var userInfo = app.Data.userInfo
      var url = 'money/Recorduser/userAdd'
      var uRecord = {
         User_ID: userInfo.User_ID,
         Abstract: '91商城消费',
         Payer: 'Biz_' + 91,
         Money: -_data.money,
         Pay_From: 'wx_pay',
         Pay_To: '91_pay',
         isMall: 1
      }
      co.getJyhsApi(url, uRecord, function (res) {
         if (typeof (callback) == 'function') {
            callback()
         }
         if (res.code == 0) {

         } else {
            co.modal(res.msg)
         }
      })
   },
   //添加商家支付记录
   addBizRecord: function () {
      var userInfo = app.Data.userInfo
      var url = 'money/Recordbiz/bizAdd'
      var bizRecord = {
         Biz_ID: 91,
         Abstract: userInfo.User_NickName + '91商城购物',
         Payer: 'User_' + userInfo.User_ID,
         Money: _data.money,
         Pay_From: 'wx_pay',
         Pay_To: '91_pay',        
      }
      co.getJyhsApi(url, bizRecord, function(rs) {
         if (rs.code == 0) {
            //co.modal(res.msg)
         } else {
            //co.modal(res.msg)
         }
      })
   },
   //忘记密码
   retrievePwd: function () {
      wx.navigateTo({
         url: '/04myCenter/02set/02changePayPwd/01retrievePassword/retrievePassword',
      })
   },
   // 微信支付
   pay: function (money) {
      //金额为订单总价
      var appid = app.Data.app.appid
      var param = { //e.detail.value
         openid: app.Data.userInfo.Wx_Openid[appid],
         appid: appid,
         money: money,
         abstract: "旧约商城购物"
      }
      //console.log('微信param', param)
      co.getShareApi('api/Wxpay/createParam', param, function (res) {
         res.data.success = function (result) {
            //for (var i in orderID) {
            //添加用户支付记录
            _this.addUserRecord(function (res) {
               //添加商家支付记录。确保数据不易出错，按顺序先加付款方数据，再加收款方数据
               _this.addBizRecord()
            })
            _this.changeOrderStatus(_data.orderID,2)//修改订单状态
            co.toastAllText('支付成功')
            
            // }
         }
         res.data.fail = function (result) {
            co.toastAllText('支付失败')
            console.log('【调起支付失败res】', result)
         }
         //调起微信支付
         wx.requestPayment(res.data)
      })
   },
   // 余额支付
   yuePay: function (money, pa) {
      var param = {
         Pay_From_ID: 'User_' + app.Data.userInfo.User_ID,
         Pay_Password: pa,
         Money: money,
         Pay_To_ID: 'Biz_' + 91,
         Abstract: '91商城购物',
         isMall: 1
      }
      co.getJyhsApi('money/Recorduser/balancePay', param, function (res) {
         if (res.code == 0) {
            co.updateUserBalance(res.data.New_Balance) //修改用户余额
            app.getWXUserInfo()//更新用户数据
            _this.changeOrderStatus(_data.orderID, 2)//修改订单状态
            co.toastAllText('支付成功')
            _this.setData({
               pasInput: false
            })
           
         } else {
            co.toastAllText('支付失败')
         }
      })
   },
   // 余额支付输入密码
   pasForm: function (e) {
      console.log("【获取密码】===", e)
      var pa = M.md5(e.detail.value.pa)      
      this.yuePay(_data.money, pa)      
   },
   onReachBottom:function(){
      id = _data.id
      this.getOrderStatus(id)
   }
})